package com.qq.client.view;

import com.qq.client.model.ManageClientConServerThread;
import com.qq.client.model.ManageQqFriendList;
import com.qq.client.model.QqClientUser;
import com.qq.common.Message;
import com.qq.common.MessageType;
import com.qq.common.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * @author weiBin
 * @date 2019/1/8
 */
public class QqClientLogin extends JFrame implements ActionListener {
    /**
     * 北部组件
     */
    JLabel jbl1;
    /**
     * 中部组件
     */
    JTabbedPane jtp;
    JPanel jp2, jp3, jp4;
    JLabel jp2_jbl1, jp2_jbl2, jp2_jbl3, jp2_jbl4;
    JButton jp2_jb1;
    JTextField jp2_jtf;
    JPasswordField jp2_jpf;
    JCheckBox jp2_jcb1, jp2_jcb2;
    /**
     * 南部组件
     */
    JPanel jpl;
    JButton jpl_jb1, jpl_jb2, jpl_jb3;

    public static void main(String[] args) {
        QqClientLogin qqClient = new QqClientLogin();
    }

    public QqClientLogin() {
        //处理北部
        jbl1 = new JLabel(new ImageIcon(QqClientLogin.class.getResource("/image/tou.gif")));
        //处理中部
        jp2 = new JPanel(new GridLayout(3, 3));
        jp2_jbl1 = new JLabel("QQ号码", JLabel.CENTER);
        jp2_jbl2 = new JLabel("QQ密码", JLabel.CENTER);
        jp2_jbl3 = new JLabel("忘记密码", JLabel.CENTER);
        jp2_jbl3.setForeground(Color.BLUE);
        jp2_jbl4 = new JLabel("申请密码保护", JLabel.CENTER);
        jp2_jbl4.setForeground(Color.BLUE);
        jp2_jb1 = new JButton(new ImageIcon(QqClientLogin.class.getResource("/image/clear.gif")));
        jp2_jtf = new JTextField();
        jp2_jpf = new JPasswordField();
        jp2_jcb1 = new JCheckBox("隐身登录");
        jp2_jcb2 = new JCheckBox("记住密码");

        jp2.add(jp2_jbl1);
        jp2.add(jp2_jtf);
        jp2.add(jp2_jb1);
        jp2.add(jp2_jbl2);
        jp2.add(jp2_jpf);
        jp2.add(jp2_jbl3);
        jp2.add(jp2_jcb1);
        jp2.add(jp2_jcb2);
        jp2.add(jp2_jbl4);
        //创建选项卡窗口
        jtp = new JTabbedPane();
        jtp.add("QQ号码", jp2);
        jp3 = new JPanel();
        jtp.add("手机号码", jp3);
        jp3 = new JPanel();
        jtp.add("电子邮件", jp4);
        //处理南部
        jpl = new JPanel();
        jpl_jb1 = new JButton(new ImageIcon(QqClientLogin.class.getResource("/image/denglu.gif")));
        jpl_jb1.addActionListener(this);
        jpl_jb2 = new JButton(new ImageIcon(QqClientLogin.class.getResource("/image/quxiao.gif")));
        jpl_jb3 = new JButton(new ImageIcon(QqClientLogin.class.getResource("/image/xiangdao.gif")));
        jpl.add(jpl_jb1);
        jpl.add(jpl_jb2);
        jpl.add(jpl_jb3);
        //添加北部组件
        this.add(jbl1, "North");
        //添加中部组件
        this.add(jtp, "Center");
        //添加南部组件
        this.add(jpl, "South");
        this.setSize(350, 240);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setTitle("用户登录");
    }

    @Override
    public void actionPerformed(ActionEvent evt) {
        //点击登陆
        if (evt.getSource() == jpl_jb1) {
            User user = new User();
            user.setPasswd(new String(jp2_jpf.getPassword()));
            user.setNo(jp2_jtf.getText().trim());
            QqClientUser qqClientUser = new QqClientUser();
            if (qqClientUser.checkUser(user)) {
                try {
                    //把创建好友列表的语句提前.
                    QqFriendList qqList = new QqFriendList(user.getNo());
                    ManageQqFriendList.addQqFriendList(user.getNo(), qqList);
                    //发送一个要求返回在线好友的请求包.
                    ObjectOutputStream oos = new ObjectOutputStream
                            (ManageClientConServerThread.getClientConServerThread(user.getNo()).getSocket().getOutputStream());

                    //做一个Message
                    Message m = new Message();
                    m.setMesType(MessageType.message_get_onLineFriend);
                    //指明我要的是这个qq号的好友情况.
                    m.setSender(user.getNo());
                    oos.writeObject(m);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //关闭当前登陆页
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(this, "用户名密码错误");
            }
        }
    }
}
